package com.egao.orderB.biz.impl;

import com.egao.common.core.damon.constant.UserConstant;
import com.egao.common.core.damon.results.BaseDao;
import com.egao.common.core.damon.results.PageResult2;
import com.egao.common.core.damon.utils.Utils;
import com.egao.common.system.entity.User;
import com.egao.orderB.biz.DriverOilRechargeBiz;
import com.egao.orderB.entity.DriverOilRecharge;
import com.egao.common.core.web.JsonResult;
import org.apache.poi.ss.formula.functions.T;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.RowMapper;
import org.springframework.stereotype.Repository;

import java.util.HashMap;
import java.util.Map;
import java.util.List;

/**
 *
 * 数据层实现类
 * @author : Doman
 * @date : 2021-07-07 16:53:48
 * @version ：1.0
 */
@Repository
public class DriverOilRechargeBizImpl extends BaseDao implements DriverOilRechargeBiz {

	@Override
	public JsonResult list(PageResult2<T> page, DriverOilRecharge driverOilRecharge, User loginUser) throws Exception {
		Map<String, String> map  = new HashMap<String, String>();
		RowMapper<DriverOilRecharge> rm = new BeanPropertyRowMapper<DriverOilRecharge>(DriverOilRecharge.class);
		StringBuffer sql = new StringBuffer("select d.id,d.organization_id,d.company_name");
		sql.append(",d.driver_id,d.driver,d.car_no,d.oil_card");
		sql.append(",d.type,d.re_date");
		sql.append(",d.price,d.oil,d.remark,d.create_time");
		sql.append(",d.create_user,d.update_time,d.update_user");
		sql.append(",sogzn.organization_name organizationName,suser.username createName ");
		sql.append(" from chf_driver_oil_recharge d ");
		sql.append(" inner join sys_user suser on suser.user_id=d.create_user ");
		sql.append(" inner join sys_organization sogzn on sogzn.organization_id=d.organization_id and sogzn.deleted=0 ");
		sql.append(" where d.deleted=0 ");
		if(loginUser.getUserId() != UserConstant.ADMIN_ID){
				sql.append(" and d.organization_id=:tableOrganizationId ");
				map.put("tableOrganizationId", loginUser.getOrganizationId() + "");
		}
		if(Utils.isNotEmpty(driverOilRecharge)){
			if(Utils.isNotEmpty(driverOilRecharge.getType())){
				sql.append(" and instr(d.type,:type)>0 ");
				map.put("type", driverOilRecharge.getType());
			}
			if(Utils.isNotEmpty(driverOilRecharge.getCompanyName())){
				sql.append(" and instr(d.company_name,:company_name)>0 ");
				map.put("company_name", driverOilRecharge.getCompanyName());
			}
			if(Utils.isNotEmpty(driverOilRecharge.getCompanyName())){
				sql.append(" and instr(d.company_name,:company_name)>0 ");
				map.put("company_name", driverOilRecharge.getCompanyName());
			}
			if(Utils.isNotEmpty(driverOilRecharge.getCarNo())){
				sql.append(" and instr(d.car_no,:car_no)>0 ");
				map.put("car_no", driverOilRecharge.getCarNo());
			}
			if(Utils.isNotEmpty(driverOilRecharge.getDriver())){
				sql.append(" and instr(d.driver,:driver)>0 ");
				map.put("driver", driverOilRecharge.getDriver());
			}
			if(Utils.isNotEmpty(driverOilRecharge.getDate1())){
				sql.append(" and d.re_date>= :date1 ");
				map.put("date1", driverOilRecharge.getDate1());
			}
			if(Utils.isNotEmpty(driverOilRecharge.getDate2())){
				sql.append(" and d.re_date<=:date2 ");
				map.put("date2", driverOilRecharge.getDate2());
			}
			if(Utils.isNotEmpty(driverOilRecharge.getSort())&&Utils.isNotEmpty(driverOilRecharge.getOrder())){
					sql.append(" order by d."+driverOilRecharge.getSort()+" "+driverOilRecharge.getOrder());
			} else {
					sql.append(" order by d.create_time desc");
			}
		}
		return jsonResult(getPageQueryList(page, sql.toString(), map, rm));
	}

	@Override
	public List<DriverOilRecharge> list(DriverOilRecharge driverOilRecharge, User loginUser) {
		Map<String, String> map  = new HashMap<String, String>();
		RowMapper<DriverOilRecharge> rm = new BeanPropertyRowMapper<DriverOilRecharge>(DriverOilRecharge.class);
		StringBuffer sql = new StringBuffer("select d.id,d.organization_id,d.company_name");
		sql.append(",d.driver_id,d.driver,d.car_no,d.oil_card");
		sql.append(",d.type,d.re_date");
		sql.append(",d.price,d.oil,d.remark,d.create_time");
		sql.append(",d.create_user,d.update_time,d.update_user");
		sql.append(",sogzn.organization_name organizationName,suser.username createName ");
		sql.append(" from chf_driver_oil_recharge d ");
		sql.append(" inner join sys_user suser on suser.user_id=d.create_user ");
		sql.append(" inner join sys_organization sogzn on sogzn.organization_id=d.organization_id and sogzn.deleted=0 ");
		sql.append(" where d.deleted=0 ");
		if(loginUser.getUserId() != UserConstant.ADMIN_ID){
			sql.append(" and d.organization_id=:tableOrganizationId ");
			map.put("tableOrganizationId", loginUser.getOrganizationId() + "");
		}
		if(Utils.isNotEmpty(driverOilRecharge)){
			if(Utils.isNotEmpty(driverOilRecharge.getType())){
				sql.append(" and instr(d.type,:type)>0 ");
				map.put("type", driverOilRecharge.getType());
			}
			if(Utils.isNotEmpty(driverOilRecharge.getCompanyName())){
				sql.append(" and instr(d.companyName,:companyName)>0 ");
				map.put("companyName", driverOilRecharge.getCompanyName());
			}
			if(Utils.isNotEmpty(driverOilRecharge.getCarNo())){
				sql.append(" and instr(d.car_no,:car_no)>0 ");
				map.put("car_no", driverOilRecharge.getCarNo());
			}
			if(Utils.isNotEmpty(driverOilRecharge.getDriver())){
				sql.append(" and instr(d.driver,:driver)>0 ");
				map.put("driver", driverOilRecharge.getDriver());
			}
			if(Utils.isNotEmpty(driverOilRecharge.getDate1())){
				sql.append(" and d.re_date>= :date1 ");
				map.put("date1", driverOilRecharge.getDate1());
			}
			if(Utils.isNotEmpty(driverOilRecharge.getDate2())){
				sql.append(" and d.re_date<=:date2 ");
				map.put("date2", driverOilRecharge.getDate2());
			}
			if(Utils.isNotEmpty(driverOilRecharge.getSort())&&Utils.isNotEmpty(driverOilRecharge.getOrder())){
				sql.append(" order by d."+driverOilRecharge.getSort()+" "+driverOilRecharge.getOrder());
			} else {
				sql.append(" order by d.create_time desc");
			}
		}
		getMapParameter(sql, map);
		return jdbcTemplate.query(sql.toString(),map, rm);
	}

	@Override
	public DriverOilRecharge getById(DriverOilRecharge driverOilRecharge) {
		RowMapper<DriverOilRecharge> rm = new BeanPropertyRowMapper<DriverOilRecharge>(DriverOilRecharge.class);
		Map<String, String> map  = new HashMap<String, String>();
		StringBuffer sql = new StringBuffer("select d.id,d.organization_id,d.company_name");
        sql.append(",d.driver_id,d.driver,d.car_no,d.oil_card");
		sql.append(",d.type,d.re_date");
		sql.append(",d.price,d.oil,d.remark,d.create_time");
		sql.append(",d.create_user,d.update_time,d.update_user");
        sql.append(",sogzn.organization_name organizationName,suser.username createName ");
        sql.append(" from chf_driver_oil_recharge d ");
        sql.append(" inner join sys_user suser on suser.user_id=d.create_user ");
        sql.append(" inner join sys_organization sogzn on sogzn.organization_id=d.organization_id and sogzn.deleted=0 ");
        sql.append(" where d.deleted=0 and d.id=:id ");
		map.put("id", driverOilRecharge.getId());
		getMapParameter(sql, map);
		return jdbcTemplate.queryForObject(sql.toString(), map, rm);
	}

	@Override
	public boolean save(DriverOilRecharge driverOilRecharge, User loginUser) {
		Map<String, String> map  = new HashMap<String, String>();
		StringBuffer sql =new StringBuffer("insert into chf_driver_oil_recharge (");
        sql.append("organization_id,company_name,driver_id,driver");
		sql.append(",car_no,oil_card,price,oil");
		sql.append(",type,re_date");
		sql.append(",remark,create_user,create_time) values ( ");
        sql.append(":organization_id,:company_name,:driver_id,:driver");
		sql.append(",:car_no,:oil_card,:price,:oil");
		sql.append(",:type,:re_date");
		sql.append(",:remark,:create_user,now())");
        map.put("organization_id", loginUser.getOrganizationId() + "");
        map.put("company_name", driverOilRecharge.getCompanyName());
        map.put("driver_id", driverOilRecharge.getDriverId()==null?null:driverOilRecharge.getDriverId()+"");
        map.put("driver", driverOilRecharge.getDriver());
        map.put("car_no", driverOilRecharge.getCarNo());
        map.put("oil_card", driverOilRecharge.getOilCard());
        map.put("price", driverOilRecharge.getPrice());
        map.put("oil", driverOilRecharge.getOil());
        map.put("type", driverOilRecharge.getType());
        map.put("re_date", driverOilRecharge.getReDate());
        map.put("remark", driverOilRecharge.getRemark());
		map.put("create_user", loginUser.getUserId() + "");
		getMapParameter(sql, map);
		int count = jdbcTemplate.update(sql.toString(), map);
		return count > 0;
	}

	@Override
	public boolean update(DriverOilRecharge driverOilRecharge, User loginUser) {
		Map<String, String> map  = new HashMap<String, String>();
		StringBuffer sql =new StringBuffer("update chf_driver_oil_recharge set ");
		sql.append("company_name=:company_name,driver_id=:driver_id,driver=:driver ");
		sql.append(",car_no=:car_no,oil_card=:oil_card,price=:price ");
		sql.append(",type=:type,re_date=:re_date");
		sql.append(",oil=:oil,remark=:remark,update_time=now() ");
		sql.append(",update_user=:update_user where id=:id ");
		map.put("company_name", driverOilRecharge.getCompanyName());
		map.put("driver_id", driverOilRecharge.getDriverId()==null?null:driverOilRecharge.getDriverId()+"");
		map.put("driver", driverOilRecharge.getDriver());
		map.put("car_no", driverOilRecharge.getCarNo());
		map.put("oil_card", driverOilRecharge.getOilCard());
		map.put("price", driverOilRecharge.getPrice());
		map.put("oil", driverOilRecharge.getOil());
		map.put("type", driverOilRecharge.getType());
		map.put("re_date", driverOilRecharge.getReDate());
		map.put("remark", driverOilRecharge.getRemark());
		map.put("update_user", loginUser.getUserId() + "");
		map.put("id", driverOilRecharge.getId());
		getMapParameter(sql, map);
		int count = jdbcTemplate.update(sql.toString(), map);
		return count > 0;
	}

	@Override
	public boolean remove(DriverOilRecharge driverOilRecharge, User loginUser) {
		int count = 0;
		Map<String, String> map = null;
		if(driverOilRecharge.getId().length()>0){
			for(String ID : driverOilRecharge.getId().split(",")){
				map  = new HashMap<String, String>();
				if(ID==null || ID.equals("")){ break; }
				StringBuffer sql =new StringBuffer("update chf_driver_oil_recharge set deleted=1" +
						",update_user=:update_user, update_time=now() "
						+ " where id =:id ");
				map.put("update_user", loginUser.getUserId() + "");
				map.put("id", ID);
				getMapParameter(sql, map);
				count = jdbcTemplate.update(sql.toString(),map);
			}
		}
		return count > 0;
	}
}
